package com.buer.storage.api;


import com.buer.storage.business.domain.Dorder;
import com.buer.storage.business.domain.Stock;
import com.buer.storage.business.dto.DorderDto;
import com.buer.storage.business.dto.OutStorageAllDto;
import com.buer.storage.business.dto.OutStorageDto;
import com.buer.storage.business.service.DorderService;
import com.buer.storage.common.annotation.Auth;
import com.buer.storage.common.eumn.ResultEnum;
import com.buer.storage.common.utils.BaseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/out")
public class DorderController {
    @Resource
    private DorderService dorderService;

    @GetMapping("/findStock.do")
    @Auth
    public BaseResult findStock(){
        List<Stock> stock = dorderService.findStock();
        return BaseResult.build(ResultEnum.SUCCESS,stock);
    }

    //根据出库ID查询
    @Auth
    @GetMapping("/selectByAll.do")
    public BaseResult selectByAll(String dorderNum, Integer dtypeId, String prodName, String beginTime, String endTime){
        List<Dorder> dorders = dorderService.selectByAll(dorderNum, dtypeId, prodName, beginTime, endTime);
        return BaseResult.build(ResultEnum.SUCCESS,dorders);
    }


    @PostMapping("/insert.do")
    @Auth
    public BaseResult insert(@RequestBody OutStorageDto outStorageDto){
        if (dorderService.insert(outStorageDto) > 0) {

            //出库单保存成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
            //出库单保存失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    @GetMapping("/findOrderAll.do")
    @Auth
    public BaseResult findOrderAll(){
        List<DorderDto> outAll = dorderService.findOrderAll();
        return BaseResult.build(ResultEnum.SUCCESS,outAll);
    }

    //根据订单ID查询入库信息
    @GetMapping("/findOutAll.do")
    public BaseResult findOutAll(Integer outId){
        List<OutStorageAllDto> outAll = dorderService.findOutAll(outId);
        return BaseResult.build(ResultEnum.SUCCESS,outAll);
    }

    //根据入库订单ID删除出库信息
    @GetMapping("/deleteOutStorageById.do")
    @Auth
    public BaseResult deleteOutStorageById(int outId) {
        if (dorderService.deleteOutStorageById(outId) > 0) {
            //删除出库单成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //删除出库单失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //根据商品ID删除商品信息
    @GetMapping("/deleteDorderdetails.do")
    @Auth
    public BaseResult deleteDorderdetails(int deliveryId) {
        if (dorderService.deleteDorderdetails(deliveryId) > 0) {
            //删除出库订单成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //删除出库订单失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //更新ID删除商品信息
    @PostMapping("/updateDorderdetails.do")
    @Auth
    public BaseResult updateDorderdetails(@RequestBody OutStorageDto outStorageDto) {
        if (dorderService.updateDorderdetails(outStorageDto) > 0) {
            //更新出库单成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //更新出库单失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //提交商品详情进行累加
    @GetMapping("/submitAddDorderdetails.do")
    @Auth
    public BaseResult submitAddBuerStock(int outId) {
        if (dorderService.submitAddBuerStock(outId) > 0) {
            //出库成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //出库失败
        return BaseResult.build(ResultEnum.FAILURE);
    }
}
